<script setup lang="ts">
import {ref} from 'vue';
import {useRoute} from 'vue-router';
import request from "../plugins/request.js";
import {showFailToast, showSuccessToast} from "vant";
import router from "../config/router";
import {getCurrentUser} from "../service/user";

const route = useRoute()
const onSubmit = async () => {
  // 获取当前用户数据
  const currentUser = await getCurrentUser();
  // 如果没有用户 直接用户未登录
  if (!currentUser){
    showFailToast('用户未登录');
    return;
  }
  // 修改用户 使用动态参数
  const result = await request.post('/user/update',{
    "id": currentUser.id,
    [editUser.value.editKey]: editUser.value.currentValue
  })
  const {code, data} = result;
  if (code === 200 && data){
    showSuccessToast('修改成功')
    router.back()
  }else {
    showFailToast('修改失败')
  }

}
const editUser = ref({
  editKey: route.query.editKey,
  editName: route.query.editName,
  currentValue: route.query.currentValue
})
</script>
<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field v-model="editUser.currentValue" :name="editUser.editKey" :label="editUser.editName"
                 :placeholder="`请输入${editUser.editName}`" :rules="[{ required: true, message: '请填写修改内容' }]"/>
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<style scoped></style>